package com.donggua.aichebao.commonui.order;

import android.content.Context;
import android.content.res.TypedArray;
import android.sax.RootElement;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.donggua.aichebao.commonui.R;
import com.donggua.aichebao.commonui.ThreeTextViewLayout;

/**
 * Created by Administrator on 2017-09-08.
 */
//com.donggua.aichebao.commonui.order.DGOrderThreeTextView
public class DGOrderThreeTextView extends RelativeLayout {
    private TextView tvFirst;
    private TextView tvSecond;
    private TextView tvThird;
    private String strFirst;
    private String strSecond;
    private String strThree;
    private int thirdId = 1000;
    private float density = 1;
    protected static final int DEFAULT_SIZE = 15;
    protected static final int DEFAULT_TEXT_COLOR = 0Xff000000;
    public DGOrderThreeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        density = context.getResources().getDisplayMetrics().density;
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomThreeTextView);
        strFirst = ta.getString(R.styleable.CustomThreeTextView_customFirstText);
        strSecond = ta.getString(R.styleable.CustomThreeTextView_customSecondText);
        strThree = ta.getString(R.styleable.CustomThreeTextView_customThreeText);
        ta.recycle();
        init(context);
    }

    protected void init(Context context){
        tvFirst = getTextView(context);
        tvSecond = getTextView(context);
        tvThird = getTextView(context);
        RelativeLayout.LayoutParams firstLayout = getMyLayoutParams();
        firstLayout.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
        tvFirst.setLayoutParams(firstLayout);
        tvFirst.setText(strFirst);
        this.addView(tvFirst);
        RelativeLayout.LayoutParams thirdLayout = getMyLayoutParams();
        thirdLayout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
        thirdLayout.leftMargin = dpToPx(20);
        tvThird.setLayoutParams(thirdLayout);
        tvThird.setText(strThree);
        tvThird.setId(thirdId);
        this.addView(tvThird);
        RelativeLayout.LayoutParams secondLayout = getMyLayoutParams();
        tvSecond.setText(strSecond);
        secondLayout.addRule(RelativeLayout.LEFT_OF,thirdId);
        tvSecond.setLayoutParams(secondLayout);
        this.addView(tvSecond);
        Log.e("three"," strFirst is "+strFirst+" strSecond is "+strSecond+" strThree is "+strThree);
    }

    private int dpToPx(int dp){
        int px = (int)(dp*density+0.5);
        return px;
    }

    private TextView getTextView(Context context) {
        float size = context.getResources().getDimension(R.dimen.dg_order_text_size);
        int color = context.getResources().getColor(R.color.gray);
        TextView textView = new TextView(context);
        textView.setTextColor(color);
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize( TypedValue.COMPLEX_UNIT_PX,size);
        return textView;
    }

    protected  RelativeLayout.LayoutParams getMyLayoutParams(){
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
        return layoutParams;
    }

    public void setFistText(String text){
        tvFirst.setText(text);
    }

    public void setSecondText(String text){
        tvSecond.setText(text);
    }

    public void setThirdText(String text){
        tvThird.setText(text);
    }
}
